package flc.ast.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;

import com.stark.imgedit.ImgEditActivity;
import com.stark.imgedit.ModuleConfig;
import com.stark.imgedit.model.ColorTuneRange;
import com.stark.imgedit.utils.BmpUtil;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import flc.ast.BaseEditFragment;
import flc.ast.R;
import flc.ast.databinding.FragmentColorTuneBinding;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ColorTuneFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ColorTuneFragment extends BaseEditFragment<FragmentColorTuneBinding> {

    public static final int INDEX = ModuleConfig.INDEX_COLOR_TUNE;
    private float mBrightness = 1;
    private float mSaturation = 1;
    private float mContrast = 0;
    private Bitmap mRetBitmap;

    public static ColorTuneFragment newInstance() {
        ColorTuneFragment fragment = new ColorTuneFragment();
        return fragment;
    }

    @Override
    public void onShow() {
        mRetBitmap = null;
        mBrightness = 1;
        mSaturation = 1;
        mContrast = 0;
        mDataBinding.sbBrightness.setProgress(calSeekBarProgress(mDataBinding.sbBrightness,
                mBrightness, ColorTuneRange.BRIGHTNESS));
        mDataBinding.sbSaturation.setProgress(calSeekBarProgress(mDataBinding.sbSaturation,
                mSaturation, ColorTuneRange.SATURATION));
        mDataBinding.sbContrast.setProgress(calSeekBarProgress(mDataBinding.sbContrast,
                mContrast, ColorTuneRange.CONTRAST));
        clearSelection();
        mDataBinding.ivBrightness.setVisibility(View.VISIBLE);
        mDataBinding.llBrightness.setVisibility(View.VISIBLE);
    }

    @Override
    public void backToMain() {
        mImgEditActivity.mode = ImgEditActivity.MODE_NONE;
        mImgEditActivity.mImgView.setImageBitmap(mImgEditActivity.getMainBit());
        mImgEditActivity.mImgView.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mImgEditActivity.showFunc();

        if (mRetBitmap != null) {
            if (mRetBitmap != mImgEditActivity.getMainBit() && !mRetBitmap.isRecycled()) {
                mRetBitmap.recycle();
            }
            mRetBitmap = null;
        }
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_color_tune;
    }

    @Override
    protected void initView() {
        mDataBinding.tvBrightness.setOnClickListener(this);
        mDataBinding.tvContrast.setOnClickListener(this);
        mDataBinding.tvSaturation.setOnClickListener(this);
        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.ivApply.setOnClickListener(this);
        onShow();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvBrightness://亮度
                clearSelection();
                mDataBinding.ivBrightness.setVisibility(View.VISIBLE);
                mDataBinding.llBrightness.setVisibility(View.VISIBLE);
                break;
            case R.id.tvContrast://对比度
                clearSelection();
                mDataBinding.ivContrast.setVisibility(View.VISIBLE);
                mDataBinding.llContrast.setVisibility(View.VISIBLE);
                break;
            case R.id.tvSaturation://饱和度
                clearSelection();
                mDataBinding.ivSaturation.setVisibility(View.VISIBLE);
                mDataBinding.llSaturation.setVisibility(View.VISIBLE);
                break;
            case R.id.ivCancel://取消
                backToMain();
                break;
            case R.id.ivApply://确定
                applyColorTune();
                break;
        }
    }

    @Override
    protected void initData() {
        initSb();
        adjustImg();
    }

    //初始化控件
    private void initSb() {
        mDataBinding.sbBrightness.setProgress(calSeekBarProgress(mDataBinding.sbBrightness,
                mBrightness, ColorTuneRange.BRIGHTNESS));
        mDataBinding.sbSaturation.setProgress(calSeekBarProgress(mDataBinding.sbSaturation,
                mSaturation, ColorTuneRange.SATURATION));
        mDataBinding.sbContrast.setProgress(calSeekBarProgress(mDataBinding.sbContrast,
                mContrast, ColorTuneRange.CONTRAST));
    }

    //调节图片
    private void adjustImg() {
        //brightness
        mDataBinding.sbBrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mBrightness = calSeekBarRealValue(mDataBinding.sbBrightness, progress, ColorTuneRange.BRIGHTNESS);
                //0-2
                updateImageViewBitmap();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //saturation
        mDataBinding.sbSaturation.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mSaturation = calSeekBarRealValue(mDataBinding.sbSaturation, progress, ColorTuneRange.SATURATION);
                //0-2
                updateImageViewBitmap();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //contrast
        mDataBinding.sbContrast.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mContrast = calSeekBarRealValue(mDataBinding.sbContrast, progress, ColorTuneRange.CONTRAST);
                //0-360
                updateImageViewBitmap();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void applyColorTune() {
        if (mRetBitmap == null) {
            backToMain();
            return;
        }
        mImgEditActivity.changeMainBitmap(mRetBitmap, true);
        backToMain();
    }

    private void updateImageViewBitmap() {
        Bitmap preRetBitmap = mRetBitmap;
        mRetBitmap = BmpUtil.adjust(mImgEditActivity.getMainBit(), mBrightness, mSaturation, mContrast);
        if (mRetBitmap != null) {
            mImgEditActivity.mImgView.setImageBitmap(mRetBitmap);
            if (preRetBitmap != null && !preRetBitmap.isRecycled() && preRetBitmap != mImgEditActivity.getMainBit()) {
                preRetBitmap.recycle();
            }
        }
    }

    private float calSeekBarRealValue(SeekBar seekBar, int progress, ColorTuneRange range) {
        int progressRange = seekBar.getMax();
        float value = progress * 1.0f / progressRange * range.getRange() + range.getMin();
        return value;
    }

    private int calSeekBarProgress(SeekBar seekBar, float value, ColorTuneRange range) {
        float valuePercent = (value - range.getMin()) / range.getRange();
        int progress = (int) (seekBar.getMax() * valuePercent);
        return progress;
    }

    private void clearSelection() {
        mDataBinding.ivBrightness.setVisibility(View.INVISIBLE);
        mDataBinding.ivContrast.setVisibility(View.INVISIBLE);
        mDataBinding.ivSaturation.setVisibility(View.INVISIBLE);
        mDataBinding.llBrightness.setVisibility(View.GONE);
        mDataBinding.llContrast.setVisibility(View.GONE);
        mDataBinding.llSaturation.setVisibility(View.GONE);
    }

}